﻿using System.Collections.Generic;
using System.Linq;
using VQData;
using VQWEB.common;

namespace VQWEB.huongnghiep.BL
{
    public class ProfessionData
    {
        #region Quiz
        public object GetQuizByType(string type)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var obj = layer.Get<hn_quiz>(
                n => n.quiz_value == type,
                c => new
                         {
                             c.quiz_id,
                             c.quiz_value,
                             c.quiz_noidung,
                             chuyennganh = GetChuyenNganhByQuizId(c.quiz_id)
                         }
                );

            return obj;
        }
        public IList<object> GetChuyenNganhByQuizId(int quiz_id)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_chuyennganh>(
                null,
                null,
                n => n.hn_quiz_chuyennganhs.Any(x => x.quiz_id == quiz_id),
                null,
                c => new
                         {
                             c.chuyennganh_id,
                             c.chuyennganh_ten,
                             c.chuyennganh_ma,
                             url = Ulties.FormatChuyenNganhUrl(c.chuyennganh_id, c.chuyennganh_ten)
                         }
                );

            return list;
        }
        #endregion

        #region Hướng nghiệp
        //View Truong
        public object ViewTruong(int truongid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var obj = layer.Get<hn_truong>(
                t=>t.truong_id == truongid,
                c => new
                    {
                        Id = c.truong_id,
                        Ten = c.truong_ten,
                        Logo = string.IsNullOrEmpty(c.truong_logo) ? "/img/no.jpg" : c.truong_logo,
                        DiaChi = c.truong_diachi,
                        DienThoai=c.truong_dienthoai,
                        Email = c.truong_email,
                        Website = c.truong_website,
                        NoiDung = c.truong_gioithieu,
                        ChuyenNganh = GetChuyenNganhByTruongId(c.truong_id)
                    }
                );

            return obj;
        }
        public object GetTruongDetail(int truongid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var obj = layer.Get<hn_truong>(
                t => t.truong_id == truongid,
                c => new
                         {
                             c.truong_id,
                             c.tinh_id,
                             c.truong_ma,
                             c.truong_ten,
                             c.truong_logo,
                             c.truong_gioithieu,
                             c.truong_diachi,
                             c.truong_website,
                             c.truong_email,
                             c.truong_dienthoai,
                             c.truong_bosung_0,
                             c.truong_bosung_1,
                             c.truong_bosung_2,
                             c.truong_bosung_3,
                             c.truong_bosung_4,
                             c.truong_bosung_5,
                             c.truong_bosung_6,
                             c.truong_bosung_7,
                             c.truong_bosung_8,
                             c.truong_bosung_9,
                             c.truong_bosung_10,
                             c.truong_bosung_11,
                             c.truong_bosung_12,
                             c.truong_bosung_13,
                             c.truong_bosung_14,
                             c.truong_bosung_15,
                             c.truong_bosung_16,
                             c.truong_bosung_17,
                             c.truong_bosung_18,
                             c.truong_bosung_19,
                             c.truong_bosung_20,
                             c.truong_bosung_21,
                             c.truong_bosung_22,
                             c.truong_bosung_23,
                             c.truong_bosung_24,
                             c.truong_bosung_25,
                             c.truong_bosung_26,
                             c.truong_bosung_27,
                             c.truong_bosung_28,
                             c.truong_bosung_29,
                             c.truong_bosung_30
                         });

            return obj;
        }
        //List Chuyen nganh by TruongId
        public IList<object> GetChuyenNganhByTruongId(int truongid)
        {

            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_chuyennganh>(null,
                null,
                x => x.hn_chuyennganh_truongs.Any(o => o.truong_id == truongid),
                null,
                c => new
                    {
                        Id = c.chuyennganh_id,
                        Ten = c.chuyennganh_ten,
                        Ma = c.chuyennganh_ma,
                        Url = Ulties.FormatChuyenNganhUrl(c.chuyennganh_id, c.chuyennganh_ten)
                    }
                );

            return list;
        }
        //View Doanh Nghiep
        public object ViewDoanhNghiep(int doanhnghiepid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var obj = layer.Get<hn_doanhnghiep>(
                t => t.doanhnghiep_id == doanhnghiepid,
                c => new
                    {
                        Id = c.doanhnghiep_id,
                        Ten = c.doanhnghiep_ten,
                        Logo = string.IsNullOrEmpty(c.doanhnghiep_logo) ? "/img/no.jpg" : c.doanhnghiep_logo,
                        c.doanhnghiep_logo,
                        DiaChi = c.doanhnghiep_diachi,
                        DienThoai = c.doanhnghiep_dienthoai,
                        Email = c.doanhnghiep_email,
                        Website = c.doanhnghiep_website,
                        NoiDung = c.doanhnghiep_gioithieu,
                        ChuyenNganh = GetDaoTaoByDoanhNghiepId(c.doanhnghiep_id)
                    }
                );

            return obj;
        }
        //List Chuyen nganh by Doanh nghiep
        public IList<object> GetDaoTaoByDoanhNghiepId(int doanhnghiepid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_chuyennganh>(null,
                null,
                x => x.hn_chuyennganh_doanhnghieps.Any(o => o.doanhnghiep_id == doanhnghiepid),
                null,
                c => new
                    {
                        Id = c.chuyennganh_id,
                        Ten = c.chuyennganh_ten,
                        Ma = c.chuyennganh_ma,
                        Url = Ulties.FormatChuyenNganhUrl(c.chuyennganh_id, c.chuyennganh_ten)
                    }
                );

            return list;
        }
        //Search Truong
        public IList<object> SearchTruong(int page, int pagesize, string key, int nhomnganh, int nganh, int he, int khoi, int tinh, double diem)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_truong>(
                page,
                pagesize,
                n => (n.truong_ten.ToLower().Contains(key.ToLower()) || string.IsNullOrEmpty(key))
                    && ((nhomnganh == -1 && nganh==-1) || ((nganh==-1)? n.hn_chuyennganh_truongs.Any(x => x.hn_chuyennganh.hn_nganh.hn_nhomnganh.nhomnganh_id == nhomnganh):n.hn_chuyennganh_truongs.Any(x => x.hn_chuyennganh.hn_nganh.nganh_id==nganh)))
                    && (he == -1 || n.hn_hedaotao_truongs.Any(o =>o.hedaotao_id ==he))
                    && (khoi == -1 || n.hn_khoi_truongs.Any(k=>k.khoi_id ==khoi))
                    && (tinh == -1 || n.tinh_id == tinh)
                    && (diem < 0 || n.hn_diem_truongs.Any(d => d.hn_diem.diem_chuan <= diem)),
                    null,
                    c => new
                    {
                        Id = c.truong_id,
                        Ten = c.truong_ten,
                        Logo = c.truong_logo,
                        KhuVuc = c.hn_tinhthanh.tinh_ten,
                        MoTa = c.truong_gioithieu,
                        Url = Ulties.FormatTruongUrl(c.truong_id,c.truong_ten)
                    }
                );

            return list;
        }
        //Huong nghiep
        public IList<object> GetAllNhomNganhAndNganh()
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_nhomnganh>(null,
                null,
                null,
                o=>o.nhomnganh_ten,
                c => new
                    {
                        Id = c.nhomnganh_id,
                        Name = c.nhomnganh_ten,
                        Nganh = c.hn_nganhs.Where(x => x.nhomnganh_id == c.nhomnganh_id).Select(o => new { cName = o.nganh_ten, cId = o.nganh_id })
                    }
                );

            return list;
        }
        public IList<object> GetAllNhomNganh()
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_nhomnganh>(
                null,
                null,
                null,
                o => o.nhomnganh_ten,
                c => new
                    {
                        Id = c.nhomnganh_id,
                        Name = c.nhomnganh_ten
                    }
                );

            return list;
        }
        public IList<object> GetChuyenNganhByNganhId(int nganhid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_chuyennganh>(null,
                null,
                x => x.nganh_id == nganhid,
                o=>o.chuyennganh_ten,
                c => new
                    {
                        Id = c.chuyennganh_id,
                        Name = c.chuyennganh_ten,
                        Ma = c.chuyennganh_ma,
                        Url = Ulties.FormatChuyenNganhUrl(c.chuyennganh_id, c.chuyennganh_ten)
                    }
                );

            return list;
        }
        public IList<object> GetTruongByChuyenNganhId(int? chuyennganhid, int page, int pagesize)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_truong>(
                page,
                pagesize,
                t => t.hn_chuyennganh_truongs.Any(o => o.chuyennganh_id == chuyennganhid),
                null,
                n => new
                    {
                        Id = n.truong_id,
                        Ten = n.truong_ten,
                        KhuVuc = n.hn_tinhthanh.tinh_ten,
                        Url = Ulties.FormatTruongUrl(n.truong_id,n.truong_ten)
                    }
                );

            return list;
        }
        public IList<object> GetDoanhNghiepByChuyenNganhId(int? chuyennganhid, int page, int pagesize)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_doanhnghiep>(
                page,
                pagesize,
                t => t.hn_chuyennganh_doanhnghieps.Any(o => o.chuyennganh_id == chuyennganhid),
                null,
                n => new
                {
                    Id = n.doanhnghiep_id,
                    Ten = n.doanhnghiep_ten,
                    KhuVuc = n.doanhnghiep_website,
                    Url = Ulties.FormatDoanhNghiepUrl(n.doanhnghiep_id, n.doanhnghiep_ten)
                }
                );

            return list;
        }
        public hn_nhomnganh GetNhomNganhById(int nhomnganhid)
        {
            using (var db = new HuongNghiepModelDataContext())
            {
                var Obj = db.hn_nhomnganhs.SingleOrDefault(n => n.nhomnganh_id == nhomnganhid);
                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        public hn_nganh GetNganhById(int? nganhId)
        {
            using (var db = new HuongNghiepModelDataContext())
            {
                var Obj = db.hn_nganhs.SingleOrDefault(n => n.nganh_id == nganhId);
                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        public hn_chuyennganh GetChuyenNganhById(int chuyennganhId)
        {
            using (var db = new HuongNghiepModelDataContext())
            {
                var Obj = db.hn_chuyennganhs.SingleOrDefault(n => n.chuyennganh_id == chuyennganhId);
                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        public IList<object> GetNganhByNhomId(int nhomid)
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_nganh>(
                null,
                null,
                x =>  x.nhomnganh_id == nhomid || nhomid == -1,
                o=>o.nganh_ten,
                n=> new
                    {
                        Id = n.nganh_id,
                        Name = n.nganh_ten,
                        n.nhomnganh_id
                    }
                );

            return list;
        }
        public IList<object> GetAllHeDaoTao()
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_hedaotao>(
                null,
                null,
                null,
                null,
                n => new
                    {
                        Id = n.hedaotao_id,
                        Name = n.hedaotao_ten
                    }
                );

            return list;
        }
        public IList<object> GetAllKhoiThi()
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_khoi>(
                null,
                null,
                null,
                null,
                n => new
                    {
                        Id = n.khoi_id,
                        Name = n.khoi_ten
                    }
                );

            return list;
        }
        public IList<object> GetAllTinhThanh()
        {
            var layer = new BusinessLayer<HuongNghiepModelDataContext>();
            var list = layer.List<hn_tinhthanh>(
                null,
                null,
                null,
                null,
                n => new
                    {
                        Id = n.tinh_id,
                        Name = n.tinh_ten
                    }
                );

            return list;
        }
        #endregion
        
        #region News
        //Get category by Id
        public core_category GetCategoryById(int id)
        {
            using (var db = new CoreModelDataContext())
            {
                var query = db.core_categories.SingleOrDefault(c => c.category_id.Equals(id));
                if (query != null)
                    return query;
                return null;
            }
        }
        //View core_news by Id
        public core_new GetNewsById(int id)
        {
            using (var db = new CoreModelDataContext())
            {
                var Obj = db.core_news.SingleOrDefault(n => n.news_id == id);

                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        //Related news
        public IList<object> GetRelatedNews(int categoryid, int id, int count)
        {
            var layer = new BusinessLayer<CoreModelDataContext>();
            var list = layer.List<core_new>(
                1,
                count,
                t => (t.core_category_news.Any(x => x.category_id == categoryid) || categoryid == -1)
                     && t.news_active == true && t.news_id != id,
                null,
                n => new
                {
                    n.news_id,
                    n.news_title,
                    n.news_image,
                    n.news_description,
                    n.news_date,
                    category_name = n.core_category_news.First().core_category.category_title,
                    url = Ulties.FormatNewsUrl(n.core_category_news.First().core_category.category_id,n.core_category_news.First().core_category.category_title, n.news_id, n.news_title)
                });

            return list;
        }
        public IList<object> GetNewsByCategoryId(int categoryid, int page, int pagesize)
        {
            var layer =new BusinessLayer<CoreModelDataContext>();
            var list = layer.List<core_new>(
                page,
                pagesize,
                t => (t.core_category_news.Any(x => x.category_id == categoryid) || categoryid == -1)
                     && t.news_active == true,
                null,
                n => new
                         {
                             n.news_id,
                             n.news_title,
                             n.news_image,
                             n.news_description,
                             n.news_date,
                             category_name = n.core_category_news.First().core_category.category_title,
                             url = Ulties.FormatNewsUrl(n.core_category_news.First().core_category.category_id,
                                              n.core_category_news.First().core_category.category_title, n.news_id,n.news_title)
                         });

            return list;
        }
        #endregion
    }
}